<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\table\ConstructionTable;
use think\Db;
use think\Exception;

class Construction extends BaseController{

    /**
     * page--基本信息
     */
    public function constructionList(){
        return view('construction_list');
    }

    /**
     * action--获取信息
     */
    public function getConstructionList(){
        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'CONS_NO desc';
        }

       $condition = ConstructionTable::getSessionConditions();
        try{
            $list = ConstructionTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql(true)->select();
            $list = Db::query($list);
            $total = ConstructionTable::table()->where($condition)->fetchSql(true)->count();
            $count = Db::query($total);
            $total = $count[0]['TP_COUNT'];
            return ['rows'=>$list,'total'=>$total];
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * page--新增
     */
    public function addPage(){
        $config = ConstructionTable::getConfigAdd();
        $this->assign('config',$config);
        $this->assign('data',null);

        $this->view->engine->layout('layout_popup');
        return view('construction_add');
    }

    /**
     * action--新增
     */
    public function add(){
        $data = input();
        $consNo = input('CONS_NO');
        $record = ConstructionTable::get(['CONS_NO'=>$consNo]);
        if (!empty($record)) {
            return json_encode(['code'=>'0','msg'=>'相同项目编号已存在']);
        }

        try{
            ConstructionTable::add($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * page--编辑
     */
    public function editPage(){
        $consId = input('consId');
        $data = ConstructionTable::get($consId);
        if(empty($data)){
            return '查询在建工程信息失败';
        }

        $config = ConstructionTable::getConfigEdit();
        $this->assign('config',$config);

        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('construction_edit');
    }

    /**
     * action--编辑
     */
    public function edit(){
        $consId = input('CONS_ID');
        $data = input();
        $old = ConstructionTable::get($consId);
        if(empty($old)){
            return json_encode(['code'=>'0','msg'=>'查询在建工程信息失败']);
        }
        $consNo = input('CONS_NO');
        $record = ConstructionTable::table()
            ->where(['CONS_NO'=>$consNo])
            ->where([['CONS_ID','neq',$consId]])
            ->find();

        if (!empty($record)) {
            return json_encode(['code'=>'0','msg'=>'相同项目编号已存在']);
        }
        try{
            ConstructionTable::update(['CONS_ID'=>$consId],$data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }

    /**
     * action 批量删除
     */
    public function deleteAll(){
        $list = input('list');
        Db::startTrans();
        try{
            foreach ($list as $consId){
                ConstructionTable::delete(['CONS_ID'=>$consId]);
            }
            Db::commit();
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            Db::rollback();
            return json_encode(['code'=>'0','msg'=>'操作失败:'.$e->getMessage()]);
        }
    }



    /**
     * page--高级查询
     */
    public function searchPage(){
        $this->view->engine->layout('layout_popup');
        return view('construction_search');
    }

    /**
     * 打印
     */
    public function printPage() {
        $id = input('id');
        $data = ConstructionTable::get($id);
        if(empty($data)){
            return '查询在建工程信息失败';
        }
        $config = ConstructionTable::getConfigPrint();
        $this->assign('config',$config);
        $this->assign('data',$data);
        $this->view->engine->layout('layout_popup');
        return view('construction_print');
    }

    public function addConditionSession() {
        $condition = input();
        Login::setListConditionConstruction($condition);
        return json_encode(['code'=>'200']);
    }

    public function importPage() {
        $this->view->engine->layout('layout_popup');
        return view('import');
    }

    public function import() {
        $inputData = input('data');
        $postData = base64_decode($inputData);
        $list = json_decode($postData,true);
        $start = input('start');

        Db::startTrans();
        $fieldList = ConstructionTable::CONSTRUCTION_FIELD_LIST;
        $fieldCount = count($fieldList);
        foreach ($list as $key => $row) {
            $data = [];

            // 字段赋值
            $i = 0;
            foreach ($row as $key1=>$value) {
                $data[$fieldList[$i]] = $value;
                $i++;
                if ($i>=$fieldCount) {
                    break;
                }
            }

            $consNo = $data['CONS_NO'];

            $record = ConstructionTable::get(['CONS_NO'=>$consNo]);
            if (!empty($record)) {
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错,相同项目编号已存在"]);
            }
            try{
                ConstructionTable::addImport($data);
            }
            catch (Exception $e){
                Db::rollback();
                return json_encode(['code'=>'0','msg'=>"第".($key + $start)."行数据写入出错",'data'=>$e->getMessage()]);
            }
        }

        Db::commit();
        return json_encode(['code'=>'200','msg'=>'导入成功']);
    }

    /**
     * page 在建工程导出页面
     */
    public function exportExcelPage() {
        $condition = ConstructionTable::getSessionConditions();

        try{
            $totalSql = ConstructionTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);

            $arr = $this->getDownLoadList($total);
            $this->assign('downList',$arr);

            $this->view->engine->layout('layout_popup');
            return view('export_excel');
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * action 在建工程导出excel
     */
    public function exportExcel(){
        $sortStr = 'CONS_NO desc';

        $start = input('start');
        $startL = $start-1;
        $end = input('end');
        $endL = $end-$start+1;
        $condition = ConstructionTable::getSessionConditions();

        try{
            $listSql = ConstructionTable::table()->where($condition)->limit($startL,$endL)->order($sortStr)->fetchSql()->select();
            $list = Db::query($listSql);
            ConstructionTable::exportExcel($list,"在建工程$start-$end");
        }
        catch (Exception $e){
            return '获取数据失败:'.$e->getMessage();
        }
    }
}